package com.ruoyi.yl.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.yl.domain.YlDrug;
import com.ruoyi.yl.mapper.DrugInfoMaintenanceMapper;
import com.ruoyi.yl.mapper.DrugMapper;
import com.ruoyi.yl.service.DrugInfoMaintenanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author: linKe
 * @Date: 2022/2/19 9:29
 * @FileName: DrugInfoMaintenanceServiceImpl
 * @version: 1.0
 * @Description: 药品信息维护
 */
@Service
@Transactional(readOnly = true)
public class DrugInfoMaintenanceServiceImpl implements DrugInfoMaintenanceService {
    @Autowired(required = false)
    private DrugInfoMaintenanceMapper drugInfoMaintenanceMapper;

    @Autowired(required = false)
    private DrugMapper drugMapper;

    /**
     * 查询所有药品
     */
    @Override
    public List<YlDrug> getAll() {

         drugMapper.selectYlDrugList(null).parallelStream().filter(new Predicate<YlDrug>() {
             @Override
             public boolean test(YlDrug ylDrug) {
                 if (ylDrug.getStock()<10) {
                     System.out.println(ylDrug);
                     return true;
                 }
                 return false;
             }
         });

        return drugMapper.selectYlDrugList(null);
    }
    /**
     * 根据处方类型查药品
     */
    @Override
    public List<YlDrug> getDrugsByType(Long id) {
        return drugMapper.selectList(new QueryWrapper<YlDrug>().eq("drug_type",id));
    }

    /**
     * 根据药品状态查药品
     */
    @Override
    public List<YlDrug> getDrugsByStatus(Long id) {
        return drugMapper.selectList(new QueryWrapper<YlDrug>().eq("drug_status",id));

    }

    /**
     * 根据创建时间查药品
     */
    @Override
    public List<YlDrug> getDrugsByTime(String date1,String date2) {

        return drugInfoMaintenanceMapper.getDrugsByTime(date1,date2);
    }
    /**
     * 根据药品名、编号、生产厂家模糊查询药品
     */
    @Override
    public List<YlDrug> getDrugsByLike(String dsc) {

        return drugInfoMaintenanceMapper.getDrugsByLike(dsc);
    }

    /**
     * 新增药品
     */
    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    public int addDrug(YlDrug drug) {
        return drugMapper.insert(drug);
    }

    /**
     * 根据药品id查药品
     */
    @Override
    public YlDrug getOneById(Long id) {
        return drugMapper.selectOne(
                new QueryWrapper<YlDrug>().eq("number",id));
    }
    /**
     * 根据药品id修改药品
     * 修改药品
     */
    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ,rollbackFor = Exception.class)
    public void updateDrug(Long id,YlDrug drug) {

        drugInfoMaintenanceMapper.updateDrug(id,drug);
    }

}
